package com.example.misqliteapp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText et1, et2;
	MiAdministradorSQLite admin;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    et1 = (EditText) findViewById(R.id.editTextDNI);
        et2 = (EditText) findViewById(R.id.editTextNombre);
		admin = new MiAdministradorSQLite(this,"mibase", null, 1);
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void crear(View v) 
	 {
		 	SQLiteDatabase bd = admin.getWritableDatabase();
		 	String dni = et1.getText().toString();
		 	String nombre = et2.getText().toString();
		 	ContentValues registro = new ContentValues();
		 	registro.put("dni", dni);
		 	registro.put("nombre", nombre);
		 	bd.insert("persona", null, registro);
		 	bd.insert("user", null, registro);
		 	bd.close();
		 	et1.setText("");
		 	et2.setText("");
		 	Toast.makeText(this, "Los datos se cargaron exitosamente",
		 	Toast.LENGTH_SHORT).show();
	    }

	    public void consultar(View v) {

	        SQLiteDatabase bd = admin.getReadableDatabase();
	        String dni = et1.getText().toString();
	        Cursor fila = bd.rawQuery(
	                "select nombre from persona where dni=" + dni, null);
	        if (fila.moveToFirst()) {
	            et2.setText(fila.getString(0));

	        } else
	            Toast.makeText(this, "No hay registro del DNI ingresado",
	                    Toast.LENGTH_SHORT).show();
	        bd.close();

	    }
	    
	    

	    public void borrar(View v) {
	    	SQLiteDatabase bd = admin.getWritableDatabase();
	        String dni = et1.getText().toString();
	        int cant = bd.delete("persona", "dni=" + dni, null);
	        bd.close();
	        et1.setText("");
	        et2.setText("");
	        if (cant == 1)
	            Toast.makeText(this, "Los datos se han borrado con �xito",
	                    Toast.LENGTH_SHORT).show();
	        else
	            Toast.makeText(this, "No existe una persona con dicho documento",
	                    Toast.LENGTH_SHORT).show();
	    }

	    public void actualizar(View v) {
	        SQLiteDatabase bd = admin.getWritableDatabase();
	        String dni = et1.getText().toString();
	        String nombre = et2.getText().toString();
	        ContentValues registro = new ContentValues();
	        registro.put("dni", dni);
	        registro.put("nombre", nombre);
	        int cant = bd.update("persona", registro, "dni=" + dni, null);
	        bd.close();
	        if (cant == 1)
	            Toast.makeText(this, "se modificaron los datos", Toast.LENGTH_SHORT)
	                    .show();
	        else
	            Toast.makeText(this, "no existe una persona con dicho documento",
	                    Toast.LENGTH_SHORT).show();
	    }
	    
	    public void lanzar(View view) {
	        Intent i = new Intent(this, ListActivity.class );
	        startActivity(i);
	    } 

}
